from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import render,redirect,HttpResponse

class AuthMiddleware(MiddlewareMixin):
    def process_request(self,request):
        #先排除不需要登录就能访问的页面
        # if request.path_info == "/login/":
        if request.path_info in ["/login/",'/image/code/']:
            return #(=return None)  返回None代表继续进行下一步
        info_dict = request.session.get("info")  #如果没登录过，则会返回None,没有登录信息，返回登录页面
        if info_dict:
            return 
        
        #没有登录信息，返回登录页面
        return redirect('/login/')

























# class M1(MiddlewareMixin):
#     "中间件1"
#     def process_request(self,request):

#         print("M1进来了")        
#         # return HttpResponse

#     def process_response(self, request,response):
#         print("M1 走了")
#         return response


# class M2(MiddlewareMixin):
#     "中间件2"
#     def process_request(self,request):
#         print("M2进来了")

#     def process_response(self, request,response):
#         print("M2 走了")
#         return response